/**
 * Created by Administrator on 2017/4/16 0016.
 */
mvc.view = {};
mvc.view.getCategories = function (data) {
    var obj=JSON.parse(data);
    for(var i in obj){
        var categoryName  =  obj[i]['categories_name'];
        var categoryId    =  obj[i]['categories_id'];

        var li = $('<li></li>');
        var a  = $('<a></a>');

        a.html(categoryName);
        li.append(a);
        li.attr('name',categoryId);
        $('.categories').append(li);
        
        li.click(function () {
            $('.post').css('display','none');
            $('.page').css('display','none');
            var id = $(this).attr('name');
            console.log(id);
            mvc.controller.getArticleList01(id)
            mvc.controller.getImage01(id);
        });
    }

}
mvc.view.getAbout = function (data) {
    var obj = JSON.parse(data);
    var div = $('<div></div>')
    var about_text = obj[0]['about_shortContent'];
    $('#about').html(about_text.substring(0,50)+'.....');
    $('#about').after(div);
    div.html("<a href='about.html'>Read More</a>");
}
mvc.view.getImg = function (data) {
    var obj=JSON.parse(data);
    var categories_id  = obj[0]['categories_id']
    var categories_name = obj[0]['categories_name'];
    var categories_introduce = obj[0]['categories_introduce'];


    $('#categories_img').attr('src','getImage?id='+categories_id);
    $('#categories_name').html(categories_name);
    $('#categories_introduce').html(categories_introduce);

}
mvc.view.getArticleList = function (data) {

    var obj = JSON.parse(data);
    if(obj[1]==0){
        var div = $('<div></div>');
        $('#blog').after(div);
        div.attr('class','post');
        var h1      = $('<h1></h1>');
        div.append(h1);
        h1.html('无数据');
    }else{
        for(var i=1;i<obj[1]+1;i++){
            var a = $('<a></a>');
            $('#page').append(a);
            a.attr('class','page');
            a.attr('name',i);
            a.attr('categories_id',obj[0][0]['article_categories']);
            a.html(i);
            a.click(function () {
                $('.post').css('display','none');
                $('.page').css('display','none');
                var page = $(this).attr('name');
                var id = $(this).attr('categories_id');
                mvc.controller.getArticleList02(id,page)
            });
        }

        for(var j in obj[0]){
            //console.log(obj[0][j]['articel_shortConetent']);
            var article = $('<article></article>');
            var header  = $('<header></header>');
            var h3      = $('<h3></h3>');
            var p       = $('<p></p>');
            var spam    = $('<spam></spam>');
            var footer  = $('<footer></footer>')
            var a       = $('<a></a>');
            var article_introduce=obj[0][j]['article_introduce'];
            $('#blog').after(article);
            article.attr('class','post');
            article.attr('id','article');
            article.attr('article_id',obj[0][j]['article_id']);
            article.append(header);
            header.append(h3);
            h3.html(obj[0][j]['article_title']);
            //p.html('Published on '+obj[0][j]['article_CreateTime']);
            header.after(p);
            p.html(obj[0][j]['article_shortContent'].substring(0.100)+'...');
            p.attr('id','article_content');
            $('#article_content').after(footer);
            footer.append(spam);
            spam.html('Authorname : Sakura&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Published on '+obj[0][j]['article_CreateTime']+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='article_url' name = "+obj[0][j]['article_id']+">Read full</a>");
            spam.append(a);
            $('.article_url').click(function () {
                console.log($(this).attr('name'));
               window.location.href = 'article.html?article_id='+$(this).attr('name');
            })
        }
    }
}
mvc.view.getAboutMe = function (data) {

    var obj = JSON.parse(data);
    $('#about_text').html(obj[0]['about_text']);
}
mvc.view.getArticle = function (data) {

    //console.log(data);
    var obj = JSON.parse(data);
    $('#article_title').html(obj[0]['article_title']);
    $('#article_content').html(obj[0]['article_content']);
}
mvc.view.login = function(data){
    if(data == 0){
        alert('账号或密码错误');
    }else{
        var obj = JSON.parse(data);
        localStorage.setItem('token',obj[1]);
        localStorage.setItem('admin_name',obj[0]);
        window.location.href = 'categories.html';

    }
}
mvc.view.getArticleList03= function (data) {

    var obj = JSON.parse(data);
    for (var i in obj[0]) {
        var tr = $('<tr></tr>');
        var button = $('<button></button>')
        for (var j in obj[0][i]) {
            var td = $('<td></td>');
            tr.append(td);
            td.html(obj[0][i][j]);
            tr.attr('id', obj[0][i]['article_id']);
        }
        $('#data').append(tr);
        tr.attr('class', 'tr');
        tr.css('vertical-align', 'middle');
        var id = tr.attr('id');
        $('.tr td:last').html('<a class="update" name=' + id + ' style="margin-right: 8%;text-decoration: none;color: #1b6d85" onclick="upda(this)">修改</a><a  class="delete" name=' + id + ' style="margin-right: 8%;text-decoration: none;color: #1b6d85" onclick="dele(this)">删除</a>');
        // $('.tr td:last').prev('td').html("<img style='width: 50px;height: 50px' src='getImage?id=" + id + "'alt=''>");
        $('#totalPage').html('共'+obj[1]+'页');
    }

}
mvc.view.getCategories01= function (data) {


    var obj = JSON.parse(data);
    for (var i in obj) {
        var tr = $('<tr></tr>');
        var button = $('<button></button>')
        for (var j in obj[i]) {
            var td = $('<td></td>');
            tr.append(td);
            td.html(obj[i][j]);
            tr.attr('id', obj[i]['categories_id']);
        }
        $('#data').append(tr);
        tr.attr('class', 'tr');
        tr.css('vertical-align', 'middle');
        var id = tr.attr('id');
        $('.tr td:last').html('<a class="update" name=' + id + ' style="margin-right: 8%;text-decoration: none;color: #1b6d85" onclick="upda(this)">修改</a><a  class="delete" name=' + id + ' style="margin-right: 8%;text-decoration: none;color: #1b6d85" onclick="dele(this)">删除</a>');
        $('.tr td:last').prev('td').html("<img style='width: 50px;height: 50px' src='getImage?id=" + id + "'alt=''>");



    }
}
mvc.view.deleteCategories=function (data){
     var obj= JSON.parse(data);
     if(obj == "删除成功"){
         window.location.reload();
     }
 }
mvc.view.addCategories=function (data){
    var obj= JSON.parse(data);
    if(obj == "添加分类成功"){
        window.location.href='categories.html';
    }
}
mvc.view.getCategoriesInfo=function (data){
    console.log(data);
    var obj= JSON.parse(data);
    $('#categories_name').val(obj[0]['categories_name']);
    console.log(obj[0]['categories_introduce']);
    $('#categories_introduce').val(obj[0]['categories_introduce']);
    $('#categories_img').attr('src','/getImage?id='+obj[0]['categories_id']);
    $('#categories_id').attr('value',obj[0]['categories_id']);
    $('#categories_imgName').attr('value',obj[0]['categories_img'])
}
mvc.view.deleteArticle=function (data){
    var obj= JSON.parse(data);
    //console.log(obj);
    if(obj['msg'] == "删除成功"){
        window.location.reload();
    }
}
mvc.view.getArticle01 = function (data) {
    var obj = JSON.parse(data);
    //var obj = data;
    console.log(obj);
    $('#article_title').val(obj[0]['article_title']);
    $('#article_categories').val(obj[0]['article_categories']);
    $('#article_shortContent').val(obj[0]['article_shortContent']);
    $('#hidden').val(obj[0]['article_id']);
    ue.addListener("ready", function () {
        // editor准备好之后才可以使用
        ue.setContent(obj[0]['article_content']);

    });

}
mvc.view.updateArticleInfo=function (data){;
    if(data['msg'] == "修改文章成功"){
        window.location.reload();
    }
}
mvc.view.updateCategories=function (data){
    var obj= JSON.parse(data);
    if(obj == "修改分类成功"){
        window.location.reload();
    }
}
mvc.view.addArticleInfo=function (data){
    if(data['msg'] == "添加文章成功"){
        window.location.href='articleManage.html';
    }
}
mvc.view.getAbout01 =function (data) {
    var obj = JSON.parse(data);
   console.log(data);
    $('#about_shortContent').val(obj[0]['about_shortContent']);
    ue.addListener("ready", function () {
        // editor准备好之后才可以使用
        ue.setContent(obj[0]['about_text']);

    });
}
mvc.view.updateAbout01 =function (data) {
    if(data['msg'] == "修改成功"){

        window.location.reload();
    }
}
mvc.view.sendRegisterEmail = function (data) {
    //console.log()
    alert(data['msg']);
}
mvc.view.register =function (data) {

    if(data['msg']=='登录成功'){
        localStorage.setItem('user_email',email);
        window.location.href = 'index.html';
    }else{
        alert(data['msg']);
    }
}
mvc.view.userLogin =function (data) {

    if(data['msg']=='登录成功'){
        localStorage.setItem('user_email',email);
        window.location.href = 'index.html';
    }else{
        alert(data['msg']);
    }
}